/*
 * CollectionDao.java
 *
 * Created on 4 de abril de 2006, 22:28
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import fmap.DatosCompartidos;
import fmap.model.Collection;
import fmap.utils.IConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author javi
 */
public class CollectionDao
{

    /** Creates a new instance of CollectionDao */
    public CollectionDao ()
    {
    }

    
    
    public Collection selectCollection ( int idCollection )
    {

        Collection col = null;

        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idCollection, name, descrip " +
                    "FROM FMAP.collection " +
                    "WHERE idCollection=?");
            ps.setInt ( 1, idCollection );

            ResultSet rs = ps.executeQuery ();

            if( rs.next () )
            {
                col = new Collection( rs.getInt ("idCollection"), rs.getString ("name"), rs.getString ("descrip") );
            }

            rs.close ();
            ps.close ();
//            conn.close ();

        }
        catch( Exception e)
        {
            
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () );
        }

        return col;
    }
    
    
    public Collection[] selectCollections ()
    {

        Collection [] aCol = null;

        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idCollection, name, descrip FROM fmap.collection");

            ResultSet rs = ps.executeQuery ();
            
            Vector vCol = new Vector();
            while ( rs.next () )
            {

                vCol.add ( new Collection( rs.getInt ("idCollection"), rs.getString ("name"), rs.getString ("descrip") ) );

            }

            rs.close ();
            ps.close ();
//            conn.close ();


            aCol = new Collection[ vCol.size () ];
            for( int i=0; i<vCol.size (); i++ )                
            {
                aCol[i] = (Collection)vCol.get (i);
            }

        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () );
        }

        return aCol;
    }


    public boolean insertCollection( Collection col )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();

            PreparedStatement ps = conn.prepareStatement ( "INSERT INTO fmap.collection (name,descrip) VALUES(?, ?)");

            ps.setString (1, col.getName () );
            ps.setString (2, col.getDesc ());
            
            ps.executeUpdate ();

            ps.close ();
//            conn.close ();
            return true;
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }

    }

    
    /** 
     * Actualizamos la coleccion que enviamos como parametro
     */
    public boolean updateCollection( Collection col )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();

            PreparedStatement ps = conn.prepareStatement ( "" +
                    "UPDATE fmap.collection " +
                    "SET " +
                        "name=?," +
                        "descrip=? " +
                    "WHERE idCollection="+col.getId ()
                    );

            ps.setString (1, col.getName () );
            ps.setString (2, col.getDesc ());
            
            ps.executeUpdate ();

            ps.close ();
//            conn.close ();
            return true;
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }

    }

    
    
    public boolean deleteCollection( int idCollection )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();

            // Eliminamos la coleccion
            PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.collection WHERE idCollection=?");
            ps.setInt ( 1, idCollection );
            ps.executeUpdate ();

            ps.close ();
//            conn.close ();
            return true;
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }

    }
    
}
